﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data.SqlClient;

namespace ZFine.Application.Gas
{
    public class SchoolApp
    {
        private ISchoolRepository service = new SchoolRepository();

        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="chargeRecordEntity"></param>
        public void ClientInsert(SchoolEntity schoolEntity)
        {
            schoolEntity.ClientCreate();
            service.Insert(schoolEntity);
        }
        public List<SchoolEntity> GetList(Pagination pagination)
        {
            var expression = ExtLinq.True<SchoolEntity>();
            return service.FindList(expression, pagination);
        }
        public List<SchoolEntity> GetList()
        {
            return service.IQueryable().OrderBy(t => t.F_CreatorTime).ToList();
        }
        public List<SchoolEntity> GetListByAccount(string schoolAccount)
        {
            var expression = ExtLinq.True<SchoolEntity>();
            expression = expression.And(t => t.SchoolAccount == schoolAccount);
            return service.IQueryable(expression).ToList();
        }
        /// <summary>
        /// 获得IQueryable
        /// </summary>
        /// <returns></returns>
        public IQueryable<SchoolEntity> GetIQueryable(string SchoolType)
        {
            var expression = ExtLinq.True<SchoolEntity>();
            if (!string.IsNullOrEmpty(SchoolType) && "0" != SchoolType)
            {
                expression = expression.And(t => t.SchoolType == SchoolType);
            }
            return service.IQueryable(expression);
        }
        public int GetCount()
        {
            return service.IQueryable().Count();
        }
        public SchoolEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        //提交
        public void SubmitForm(SchoolEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                service.Insert(organizeEntity);
            }
        }
        //删除
        public void Del(string keyValue)
        {
            service.Delete(a=>a.F_Id==keyValue);
        }
        //更新
        public void Update(SchoolEntity organizeEntity)
        {
            service.Update(organizeEntity);
        }

        // 取得可用学校列表
        public List<SchoolEntity> GetEnableSchoolList(string classValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT  DISTINCT d.*
                            FROM    SC_School d
                                    INNER  JOIN SC_Class i ON i.SchoolId = d.F_Id
                            WHERE   1 = 1
                                    AND i.isEnable = 1");
            if (!string.IsNullOrEmpty(classValue))
            {
                strSql.Append(@"AND i.Class = @classValue");
                DbParameter[] parameter ={
                    new SqlParameter("@classValue",classValue),
                };
                return service.FindList(strSql.ToString(), parameter);
            }

            return service.FindList(strSql.ToString());
        }
        public int GetMaxSortId()
        {
            return service.IQueryable().Max(p => p.SortId).ToInt();
        }
        //以学校账号检索数据
        public SchoolEntity GetFormBySchoolAccount(string schoolAccount)
        {
            var expression = ExtLinq.True<SchoolEntity>();
            return service.FindEntity(expression.And(t => t.SchoolAccount == schoolAccount));
        }
    }
}
